package com.witmed.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.witmed.entity.Doctor;
import com.witmed.service.DoctorService;
import com.witmed.service.impl.DoctorServiceImpl;
import com.witmed.util.PhoneCode;

/**
 * Servlet implementation class DoctorServlet
 */
@WebServlet("/Doctor")
public class DoctorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DoctorService doctorService = new DoctorServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoctorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String op = request.getParameter("op");//获取操作符
		 if ("getUserName".equals(op)) {
		    doLoginUserName(request,response);
	    }else if ("phone".equals(op)) {
	    	//System.out.println("1");
			doGetPhoneCode(request,response);
		}
}


	private void doGetPhoneCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String phone=request.getParameter("phoneNum");
		List<Doctor> list = doctorService.getPhoneLogin(phone);
		//System.out.println("2");
		//检查号码是否注册了
		if (list.isEmpty()) {
			//System.out.println("2-1");
			response.getWriter().print("noExist"); //不存在，提示去注册
			//System.out.println(list);
		} else {
			//根据获取到的手机号发送验证码
			//System.out.println("2-2");
			HttpSession session = request.getSession();
			session.setAttribute("doctor", list.get(0).getDoctorName());
			session.setAttribute("doctorId", list.get(0).getDoctorId());
			String code = PhoneCode.getPhonemsg(phone);
			System.out.println(code);
			response.getWriter().print(code);
		}
	}


	private void doLoginUserName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String realName = request.getParameter("userName");
		String passWord = request.getParameter("userPassword"); 
		List<Doctor> doctor = doctorService.getLogin(realName, passWord);
		response.setContentType("application/json;charset=utf-8");
		if (doctor.isEmpty()) {
			ObjectMapper objectMapper = new ObjectMapper();
			String json = objectMapper.writeValueAsString("fail");
			response.getWriter().print(json);
		}else {
			HttpSession session =request.getSession();
			session.setAttribute("doctor", doctor.get(0).getDoctorName());
			session.setAttribute("doctorId", doctor.get(0).getDoctorId());
			ObjectMapper objectMapper = new ObjectMapper();
			String json = objectMapper.writeValueAsString(doctor);
			response.getWriter().print(json);
		}
		
	}
	
	
}
